import {
  getData,
  setData,
  updata,
  removeDate,
  getClassiyfData
} from "../../api/msbd";
export default {
  namespaced: true,
  state: {
    data: [],
    total: 0,
    page: 1,
    pageSize: 10,
    sort: "desc",
    order: "id",
    classify: []
  },
  mutations: {
    GET_DATA(state, data) {
      state.data = data.data.map(item => {
        item.time = item.time;
        // item.time = item.time + "分钟";
        return item;
      });
      state.total = data.total;
    },
    SET_PAGE(state, page) {
      state.page = page;
    },
    SET_SORT(state, ele) {
      console.log(ele);
      if (ele.order == "descending") {
        state.sort == "desc";
        state.order == ele.prop;
      } else if (ele.order == "ascending") {
        state.sort == "asc", state.order == ele.prop;
      } else {
        state.sort == "desc";
        state.order == "id";
      }
    },
    GET_CLASSIFY(state, data) {
      state.classify = data;
    }
  },
  actions: {
    //获取数据
    async getTableData({ commit, state, dispatch }, text) {
      let dat = await getData({
        currentPage: state.page - 1,
        pageSize: state.pageSize,
        sort: state.sort,
        orderby: state.order,
        keyword: text
      });
      console.log(dat);
      commit("GET_DATA", {
        data: dat.data,
        total: dat.total[0].total
      });
      dispatch("getClassiyfData");
    },
    //新增
    async setTableData({ dispatch }, data) {
      // console.log(data);
      await setData(data);
      dispatch("getTableData");
    },
    //修改
    async upTableDate({ dispatch }, data) {
      await updata(data);
      dispatch("getTableData");
    },
    //删除
    async removeTableDate({ dispatch }, data) {
      await removeDate(data);
      dispatch("getTableData");
    },
    //排序
    sortTableData({ commit }, data) {
      commit("SET_SORT", data);
    },
    //获取类别
    async getClassiyfData({ commit }) {
      const arg = await getClassiyfData();
      commit("GET_CLASSIFY", arg.data);
    }
  }
};
